package com.source.baseData.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.source.baseData.entity.SlaughterHouse;
import com.source.baseData.entity.SlaughterHouseRepair;
import com.source.baseData.service.SlaughterHouseRepairService;
import com.source.baseData.vo.HouseRepairVO;
import com.source.baseData.vo.SlaughterHouseRepairVo;
import com.source.baseData.vo.UserRepairs;
import com.source.savings.Saving;
import com.source.sys.entity.User;
import com.source.sys.service.UserService;
import com.source.utils.CommonResult;
import com.source.utils.MyPage;
import com.source.utils.ResultEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author IceBerg777
 * @since 2021-03-21
 */
@Api(tags = "屠宰市场维修人员管理")
@RestController
@RequestMapping("/baseData/slaughter-house-repair")
public class SlaughterHouseRepairController {

    @Autowired
    SlaughterHouseRepairService houseRepairService;

    @Autowired
    UserService userService;

//    @ApiOperation(value = "给屠宰市场添加/更新维修人员(可批量，id用','隔开）")
//    @PostMapping("/slaughterHouse/addRepairs/{houseId}/{repairIds}")
//    public CommonResult<String> addRepairs(@PathVariable("houseId") String houseId,
//                                           @PathVariable("repairIds") List<String> ids) {
//        for (String id : ids) {
//            houseRepairService.saveOrUpdate(new SlaughterHouseRepair(null, houseId, id, false));
//        }
//        return new CommonResult<String>(ResultEnum.SUCCESS);
//    }

    @ApiOperation(value = "给屠宰市场添加/更新维修人员")
    @PostMapping("/slaughterHouse/addRepairs")
    public CommonResult<String> addRepairs(@RequestBody @Validated HouseRepairVO hvo) {
        try {
            List<String> ids = hvo.getUserId();
            String houseId = hvo.getHouseId();
            for (String id : ids) {
                houseRepairService.saveOrUpdate(new SlaughterHouseRepair(null, houseId, id, false));
            }
            return new CommonResult<String>(ResultEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult<String>(ResultEnum.ERROR);
        }
    }

    @ApiOperation(value = "根据关键词模糊查询")
    @GetMapping("/slaughterHouse/findByKeyWord/{keyWord}/{current}/{size}")
    public CommonResult<Page> findByKeyWord(@PathVariable(required = false) String keyWord,
                                            @PathVariable Integer current, @PathVariable Integer size) {
        if(null == keyWord){
            CommonResult<IPage> allOnPages = findAllOnPages(current, size);
            IPage data = allOnPages.getData();
            Page p = (Page) data;
            return new CommonResult<Page>(ResultEnum.SUCCESS, p);
        }
        List<SlaughterHouseRepairVo> list = houseRepairService.findByKeyWord("%" + keyWord + "%");
        Page page = MyPage.list2Page(current, size, list);

        return new CommonResult<Page>(ResultEnum.SUCCESS,page);
    }

    @ApiOperation(value = "(批量)移除屠宰市场的维修人员")
    @PostMapping("/slaughterHouse/deleteRepairs")
    public CommonResult<String> deleteRepairs(@RequestBody @Validated HouseRepairVO hvo) {
        try {
            List<String> ids = hvo.getUserId();
            String houseId = hvo.getHouseId();
            QueryWrapper<SlaughterHouseRepair> wrapper = new QueryWrapper<>();
            wrapper.eq("houseId", houseId).in("userId", ids);
            if (houseRepairService.remove(wrapper)) {
                return new CommonResult<String>(ResultEnum.SUCCESS);
            }
            return new CommonResult<String>(ResultEnum.ERROR);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult<String>(ResultEnum.ERROR,"操作异常！");
        }
    }

    @ApiOperation(value = "根据屠宰场id查询(已分配维修人员) - 返回list集合")
    @GetMapping("/slaughterHouse/findHouseRepairsByHouseId/{houseId}")
    public CommonResult<List<SlaughterHouseRepairVo>> findHouseRepairsByHouseIdOnPagination(@PathVariable(value = "houseId") String houseId) {
        List<SlaughterHouseRepairVo> list = houseRepairService.findByHouseId(houseId);
        return new CommonResult<List<SlaughterHouseRepairVo>>(ResultEnum.SUCCESS, list);
    }

    @ApiOperation(value = "根据屠宰场id查询还没有分配给它的维修人员 - 返回list集合")
    @GetMapping("/slaughterHouse/findUnSavedHouseRepairsByHouseId/{houseId}")
    public CommonResult<List<User>> findUnSavedHouseRepairsByHouseId(@PathVariable String houseId){
        List<User> list = houseRepairService.findUnSavedHouseRepairsByHouseId(houseId);
        return new CommonResult<List<User>>(ResultEnum.SUCCESS, list);
    }

    @ApiOperation(value = "查找没有任何维修人员的市场")
    @GetMapping("/findUnAdded")
    public CommonResult<List<SlaughterHouse>> findUnAdded(){
        List<SlaughterHouse> list = houseRepairService.findUnAdded();
        return new CommonResult<List<SlaughterHouse>>(ResultEnum.SUCCESS, list);
    }

    @ApiOperation(value = "根据屠宰场id查询未被选上的、已经被选上的人- 返回list")
    @GetMapping("/findRepairs/{houseId}")
    public CommonResult<List> findRepairs(@PathVariable String houseId){
        List<User> list = houseRepairService.findUnSavedHouseRepairsByHouseId(houseId);
        List<UserRepairs> userRepairs = new ArrayList<>(list.size());

        BeanUtils.copyProperties(list, userRepairs);

        for (User user : list) {
            UserRepairs userRepairs1 = new UserRepairs();
            BeanUtils.copyProperties(user, userRepairs1);
            userRepairs.add(userRepairs1);
            userRepairs1.setDisable(false);
        }


        List<SlaughterHouseRepairVo> byHouseId = houseRepairService.findByHouseId(houseId);

        for (SlaughterHouseRepairVo slaughterHouseRepairVo : byHouseId) {
            slaughterHouseRepairVo.setDisable(true);
        }

        List result = new ArrayList();
        for (UserRepairs userRepair : userRepairs) {
            result.add(userRepair);
        }
        for (SlaughterHouseRepairVo slaughterHouseRepairVo : byHouseId) {
            result.add(slaughterHouseRepairVo);
        }


        return new CommonResult<List>(ResultEnum.SUCCESS, result);
    }


//    @ApiOperation(value = "查询所有市场(已分配维修人员)，分页")
//    @GetMapping("/slaughterHouse/findHouseHasRepairs")
//    public CommonResult<IPage<SlaughterHouseRepairVo>> findAllOnPages(@RequestParam(value = "current",defaultValue = "1") Integer current,
//                                @RequestParam(value = "size", defaultValue = "10") Integer size){
//        Page<SlaughterHouseRepairVo> page = new Page<>(current,size);
//        IPage<SlaughterHouseRepairVo> pages = houseRepairService.findAll(page);
//        return new CommonResult<IPage<SlaughterHouseRepairVo>>(ResultEnum.SUCCESS, pages);
//    }

    @ApiOperation(value = "查询所有市场(已分配维修人员)，分页")
    @GetMapping("/slaughterHouse/findHouseHasRepairs/{current}/{size}")
    public CommonResult<IPage> findAllOnPages(@PathVariable("current") Integer current,
                                                                      @PathVariable("size") Integer size) {
        Page<SlaughterHouseRepairVo> page = new Page<>(current, size);
        IPage<SlaughterHouseRepairVo> pages = houseRepairService.findAll(page);
        return new CommonResult<IPage>(ResultEnum.SUCCESS, pages);
    }

    @ApiOperation(value = "查询所有市场的名字和id - 分页、去重")
    @GetMapping("/findDistinct/{current}/{size}")
    public CommonResult<Page> findDistinct(@PathVariable Integer current, @PathVariable Integer size){

        List<SlaughterHouseRepairVo> list = houseRepairService.findDistinct();
        Page page = MyPage.list2Page(current, size, list);
        return new CommonResult<Page>(ResultEnum.SUCCESS, page);
    }

    @ApiOperation(value = "查看市场分配维修人员明细")
    @GetMapping("/slaughterHouse/findRepairs/{current}/{size}")
    public CommonResult<Page<User>> findRepairs(@PathVariable("current") Integer current,
                                                @PathVariable("size") Integer size) {
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        Page<SlaughterHouseRepairVo> page = new Page<>(current, size);
        IPage<SlaughterHouseRepairVo> all = houseRepairService.findAll(page);
        List<SlaughterHouseRepairVo> records = all.getRecords();
        List<String> users = new ArrayList<>();
        for (SlaughterHouseRepairVo record : records) {
            String userId = record.getUserId();
            users.add(userId);
        }
        List<User> userId = userService.list(wrapper.in("userId", users));
        Page pages = MyPage.list2Page(current, size, userId);
        return new CommonResult<Page<User>>(ResultEnum.SUCCESS, pages);
    }


}

